<template>
  <el-dialog
    title="收款地址"
    :visible.sync="visible"
    :close-on-click-modal="false"
    :before-close="handleCloseDialog"
    width="680px"
  >
    <el-form
      :model="dataObject"
      :rules="rules"
      ref="adminForm"
      label-width="100px"
      size="small"
    >
      <el-form-item label="姓名：" prop="realName">
        <el-input
          v-model="dataObject.realName"
          clearable
          placeholder="请输入姓名"
        ></el-input>
      </el-form-item>
      <el-form-item label="手机号：" prop="phoneNo">
        <el-input
          v-model="dataObject.phoneNo"
          clearable
          placeholder="请输入手机号"
        ></el-input>
      </el-form-item>
      <el-form-item label="钱包地址：" prop="address">
        <el-input
          v-model="dataObject.address"
          clearable
          placeholder="请输入钱包地址"
        ></el-input>
      </el-form-item>
      <el-form-item label="网络：" prop="network">
        <el-select v-model="dataObject.network" placeholder="请选择状态">
          <el-option
            v-for="item in networkStatusOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button
        type="primary"
        @click="handleConfirmDialog('adminForm')"
        size="mini"
        >确 定</el-button
      >
      <el-button @click="handleCloseDialog" size="mini">取 消</el-button>
    </span>
  </el-dialog>
</template>
<script>
import { getBankInfo, updateBankInfo } from "@/api/member";
import { createNamespacedHelpers } from "vuex";

const { mapState } = createNamespacedHelpers("member");

const defaultObject = {
  realName: "",
  phoneNo: "",
  address: "",
  network: ""
};

export default {
  name: "deductionDialog",
  data() {
    return {
      visible: true,
      loading: false,
      dataObject: Object.assign({}, defaultObject)
    };
  },
  props: {
    row: {
      type: Object,
      default: {}
    }
  },
  computed: {
    ...mapState({
      rules: state => state.rules,
      networkStatusOptions: state => state.routes.networkStatus
    })
  },
  mounted() {
    getBankInfo({ memberId: this.row.id }).then(response => {
      this.dataObject = response.data || {};
    });
  },
  methods: {
    handleCloseDialog() {
      this.$emit("handle-cancel");
    },
    handleConfirmDialog(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          updateBankInfo({ ...this.dataObject, memberId: this.row.id }).then(
            response => {
              this.resetForm(formName);
              this.$message({
                message: "提交成功",
                type: "success",
                duration: 1000
              });
              this.$emit("handle-confirm");
            }
          );
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.dataObject = Object.assign({}, defaultObject);
    }
  }
};
</script>
<style scoped></style>
